package util;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import constant.Constant;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by chenkuo on 2018/3/1.
 */
public final class LogManager {

    private static BufferedReader stdout;
    private static PrintWriter printWriter;
    private static Connection connection;
    public static void init() throws IOException {
        connection = new Connection("123.57.51.92");
        connection.connect();
        boolean authenticateWithPassword = connection.authenticateWithPublicKey("kuo.chen", new File("C:\\ssh\\id_rsa_bingyao"),  "password");
        if (!authenticateWithPassword) {
            throw new RuntimeException("Authentication failed. Please check hostName, userName and passwd");
        }
        Session  session = connection.openSession();
        session.requestDumbPTY();
        session.startShell();
        stdout = new BufferedReader(new InputStreamReader(new StreamGobbler(session.getStdout()), StandardCharsets.UTF_8));
        printWriter = new PrintWriter(session.getStdin());
    }

    public static void close(){
        try {
            stdout.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        printWriter.close();
        connection.close();
    }


    public static String exeCmd(final String env, String cmd){

        final StringBuilder builder = new StringBuilder();
        try {
            init();
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    String line;
                    int _mark = 0;
                    try {
                        while ((line = stdout.readLine()) != null) {
                            builder.append(line).append("\r\n");
                            if(line.startsWith("[iss@" + env)){
                                _mark ++ ;
                                if(_mark == 3){
                                    break;
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            thread.start();
            printWriter.write("sudo -u iss -i" + "\r\n");
            printWriter.write("ssh " + env + "\r\n");
            printWriter.flush();
            printWriter.write(cmd + "\r\n");
            printWriter.flush();
            thread.join();
            close();
            return builder.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static void main(String[] args) throws Exception {
            String abc = LogManager.exeCmd("Fstable-001", "grep \"TDH2018032714293384\" /mnt/iss/service/push-server/nlogs/message.log.2018-03-27.log|grep 13691541162");
            System.out.println(abc);

    }



}